package com.ktwlsoft.archivemanage.domain.business.businessService;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.config.exception.ResultException;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.businessInspect.BusinessInspectRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.guiBusiness.GuiBusinessGuidanceRepositoryService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.recycle.DeleteRequest;
import com.ktwlsoft.archivemanage.payload.recycle.QueryRecycleRequest;
import com.ktwlsoft.archivemanage.payload.recycle.RestoreRequest;
import com.ktwlsoft.archivemanage.payload.recycle.TypeAndIds;
import com.ktwlsoft.archivemanage.service.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class RecycleService {

    /**业务检查业务层接口实现类*/
    private final BusinessInspectRepositoryService businessInspectRepositoryService;

    /**业务指导业务层接口实现类*/
    private final GuiBusinessGuidanceRepositoryService guidanceRepositoryService;

    public RecycleService(BusinessInspectRepositoryService businessInspectRepositoryService, GuiBusinessGuidanceRepositoryService guidanceRepositoryService) {
        this.businessInspectRepositoryService = businessInspectRepositoryService;
        this.guidanceRepositoryService = guidanceRepositoryService;
    }


    /**
      * 列表查询 on 2020/4/13 10:19
      *
      * @param request request
      * @return Result<?>
      */
    public Result<?> queryByPage(QueryRecycleRequest request) {
        if(request.getPn() == null || request.getPn() == 0){
            request.setPn(1);
        }
        if(request.getPageSize() == null || request.getPageSize() == 0){
            request.setPageSize(30);
        }
        if(request.getDeleteFrom() != null && !"".equals(request.getDeleteFrom().trim())){
            request.setDeleteFrom(DateUtils.dayMinTime(request.getDeleteFrom().trim()));
        }else {
            request.setDeleteFrom(null);
        }
        if(request.getDeleteTo() != null && !"".equals(request.getDeleteTo().trim())){
            request.setDeleteTo(DateUtils.dayMaxTime(request.getDeleteTo().trim()));
        }else {
            request.setDeleteTo(null);
        }
        if(request.getKeywords() != null) {
            request.setKeywords(request.getKeywords().trim());
        }
        if(request.getPath() == null){
            request.setPath(QueryRecycleRequest.ALL);
        }
        boolean b = true;
        for(String path:QueryRecycleRequest.PATHS){
            if(path.equals(request.getPath())){
                b = false;
            }
        }
        if(b){
            return Result.body(request.getPath(), ResultType.CHECK_ERROR).withMessage("路径不正确");
        }
        PageInfo<JSONObject> pageInfo = getryByPage(request);
        return Result.ok(pageInfo);
    }

    /**
      * 分页获取 on 2020/4/13 10:03
      *
      * @param request request
      * @return PageInfo<BusinessInspect>
      */
    private PageInfo<JSONObject> getryByPage(QueryRecycleRequest request) {
        PageHelper.startPage(request.getPn(),request.getPageSize());
        return new PageInfo<>(businessInspectRepositoryService.findRecycle(request));

    }

    /**
      * 批量恢复 on 2020/4/13 10:24
      *
      * @param request request
      * @return int
      */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> restore(RestoreRequest request) {
        if(request.getTypeAndIds() == null || request.getTypeAndIds().size() <= 0){
            return Result.body(request,ResultType.CHECK_ERROR).withMessage("参数为空");
        }
        try {
            for(TypeAndIds typeAndIds:request.getTypeAndIds()){
                if(typeAndIds.getType().equals(TypeAndIds.INSPECT)){
                    //业务检查
                    businessInspectRepositoryService.restore(typeAndIds.getIds());
                }else if(typeAndIds.getType().equals(TypeAndIds.GUIDANCE)){
                    //业务指导
                    guidanceRepositoryService.restore(typeAndIds.getIds());
                }else{
                    return Result.body(request,ResultType.CHECK_ERROR).withMessage("类型不正确");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new ResultException("操作过程中出现错误",ResultType.BUSINESS_ERROR);
        }
        return Result.ok(request);
    }

    /**
      * 批量删除 on 2020/4/13 10:53
      *
      * @param request request
      * @return Result<?>
      */
    public Result<?> delete(DeleteRequest request){
        if(request.getTypeAndIds() == null || request.getTypeAndIds().size() <= 0){
            return Result.body(request,ResultType.CHECK_ERROR).withMessage("参数为空");
        }
        try {
            for(TypeAndIds typeAndIds:request.getTypeAndIds()){
                if(typeAndIds.getType().equals(TypeAndIds.INSPECT)){
                    //业务检查
                    businessInspectRepositoryService.deleteByIds(typeAndIds.getIds());
                }else if(typeAndIds.getType().equals(TypeAndIds.GUIDANCE)){
                    //业务指导
                    guidanceRepositoryService.deleteByIds(typeAndIds.getIds());
                }else{
                    return Result.body(request,ResultType.CHECK_ERROR).withMessage("类型不正确");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.body(request,ResultType.BUSINESS_ERROR).withMessage("操作过程中出现错误");
        }
        return Result.ok(request);
    }
}
